"use strict";

var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard").default;

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));

var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));

var _jsxRuntime = require("react/jsx-runtime");

var _proProvider = require("@ant-design/pro-provider");

var _proUtils = require("@ant-design/pro-utils");

var _antdV = require("antd-v4");

var _moment = _interopRequireDefault(require("moment"));

var _react = _interopRequireWildcard(require("react"));

/**
 * 日期范围选择组件
 *
 * @param
 */
var FieldRangePicker = function FieldRangePicker(_ref, ref) {
  var text = _ref.text,
      mode = _ref.mode,
      format = _ref.format,
      render = _ref.render,
      renderFormItem = _ref.renderFormItem,
      plain = _ref.plain,
      showTime = _ref.showTime,
      fieldProps = _ref.fieldProps;
  var intl = (0, _proProvider.useIntl)();

  var _ref2 = Array.isArray(text) ? text : [],
      _ref3 = (0, _slicedToArray2.default)(_ref2, 2),
      startText = _ref3[0],
      endText = _ref3[1];

  var genFormatText = (0, _react.useCallback)(function (formatValue) {
    if (typeof (fieldProps === null || fieldProps === void 0 ? void 0 : fieldProps.format) === 'function') {
      var _fieldProps$format;

      return fieldProps === null || fieldProps === void 0 ? void 0 : (_fieldProps$format = fieldProps.format) === null || _fieldProps$format === void 0 ? void 0 : _fieldProps$format.call(fieldProps, formatValue);
    }

    return (fieldProps === null || fieldProps === void 0 ? void 0 : fieldProps.format) || format || 'YYYY-MM-DD';
  }, [fieldProps, format]); // activePickerIndex for https://github.com/ant-design/ant-design/issues/22158

  var parsedStartText = startText ? (0, _moment.default)(startText).format(genFormatText((0, _moment.default)(startText))) : '';
  var parsedEndText = endText ? (0, _moment.default)(endText).format(genFormatText((0, _moment.default)(endText))) : '';

  if (mode === 'read') {
    var dom = (0, _jsxRuntime.jsxs)("div", {
      ref: ref,
      children: [(0, _jsxRuntime.jsx)("div", {
        children: parsedStartText || '-'
      }), (0, _jsxRuntime.jsx)("div", {
        children: parsedEndText || '-'
      })]
    });

    if (render) {
      return render(text, (0, _objectSpread2.default)({
        mode: mode
      }, fieldProps), (0, _jsxRuntime.jsx)("span", {
        children: dom
      }));
    }

    return dom;
  }

  if (mode === 'edit' || mode === 'update') {
    var momentValue = (0, _proUtils.parseValueToMoment)(fieldProps.value);

    var _dom = (0, _jsxRuntime.jsx)(_antdV.DatePicker.RangePicker, (0, _objectSpread2.default)((0, _objectSpread2.default)({
      ref: ref,
      format: format,
      showTime: showTime,
      placeholder: [intl.getMessage('tableForm.selectPlaceholder', '请选择'), intl.getMessage('tableForm.selectPlaceholder', '请选择')],
      bordered: plain === undefined ? true : !plain
    }, fieldProps), {}, {
      value: momentValue
    }));

    if (renderFormItem) {
      return renderFormItem(text, (0, _objectSpread2.default)({
        mode: mode
      }, fieldProps), _dom);
    }

    return _dom;
  }

  return null;
};

var _default = /*#__PURE__*/_react.default.forwardRef(FieldRangePicker);

exports.default = _default;